package cn.wolfcode.wms.service.impl;

import cn.wolfcode.wms.domain.Employee;
import cn.wolfcode.wms.domain.ProductStock;
import cn.wolfcode.wms.domain.StockOutcomeBill;
import cn.wolfcode.wms.domain.StockOutcomeBillItem;
import cn.wolfcode.wms.exception.LoginException;
import cn.wolfcode.wms.mapper.ProductStockMapper;
import cn.wolfcode.wms.mapper.StockOutcomeBillItemMapper;
import cn.wolfcode.wms.mapper.StockOutcomeBillMapper;
import cn.wolfcode.wms.page.PageResult;
import cn.wolfcode.wms.qo.StockOutcomeBillQueryObject;
import cn.wolfcode.wms.service.ProductStockService;
import cn.wolfcode.wms.service.StockOutcomeBillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;

@Service
public class StockOutcomeBillServiceImpl implements StockOutcomeBillService {
    @Autowired
    private StockOutcomeBillMapper stockOutcomeBillMapper;
    @Autowired
    private StockOutcomeBillItemMapper stockOutcomeBillitenMapper;
    @Autowired
    private ProductStockService productStockService;

    public void deleteById(Long id) {
        StockOutcomeBill sib = stockOutcomeBillMapper.selectByPrimaryKey(id);
        if (sib.getStatus()==sib.STATUS_AUDITED){
            throw new LoginException("该订单已经审核无法删除");
        }
        stockOutcomeBillitenMapper.deleteByBillId(id);
        stockOutcomeBillMapper.deleteByPrimaryKey(id);
    }

    public void updateOrSave(StockOutcomeBill stockOutcomeBill) {
        if (stockOutcomeBill.getId() != null) {
           update(stockOutcomeBill);
        } else {

                save(stockOutcomeBill);
                   }
    }

    private void save(StockOutcomeBill stockOutcomeBill){

        //获取当前登录用户,设置录入相关信息
        //Employee currentUser = UserContext.getCurrentUser();
        Employee employee = new Employee();
        employee.setId(1L);
        stockOutcomeBill.setInputUser(employee);
        stockOutcomeBill.setInputTime(new Date());
        //重置订单状态为未审核h
        stockOutcomeBill.setStatus(stockOutcomeBill.STATUS_NORMAL);
        //计算总金额与总数量
        BigDecimal totalAmount = BigDecimal.ZERO;
        BigDecimal totalNumber = BigDecimal.ZERO;
        //遍历明细列表,用与计算金额小鸡,设置到明细对象中
        for (StockOutcomeBillItem item :stockOutcomeBill.getItems()){
            //计算明细金额小鸡,设置到明细对象中
            BigDecimal amount = item.getNumber().multiply(item.getSalePrice()).setScale(2, RoundingMode.HALF_UP);
            item.setAmount(amount);
            totalAmount = totalAmount.add(amount);
            totalNumber = totalNumber.add(item.getNumber());
        }
        //为订单对象设置总金额与数量
        stockOutcomeBill.setTotalAmount(totalAmount);
        stockOutcomeBill.setTotalNumber(totalNumber);
        //保存订单
        stockOutcomeBillMapper.insert(stockOutcomeBill);
        //为明细对象设置订单id,保存明细
        for (StockOutcomeBillItem item :stockOutcomeBill.getItems()){
            item.setBillId(stockOutcomeBill.getId());
            stockOutcomeBillitenMapper.insert(item);
        }
    }
    private void update(StockOutcomeBill stockOutcomeBill){
        StockOutcomeBill sib = stockOutcomeBillMapper.selectByPrimaryKey(stockOutcomeBill.getId());
        if (sib.getStatus()==stockOutcomeBill.STATUS_AUDITED){
            throw new LoginException("该订单已经审核无法更新");
        }
        //删除就明细
        stockOutcomeBillitenMapper.deleteByBillId(stockOutcomeBill.getId());
        //计算商品总金额和数量
        BigDecimal totalAmount = BigDecimal.ZERO;
        BigDecimal totalNumber = BigDecimal.ZERO;
        //遍历明细列表,用与计算金额小鸡,设置到明细对象中
        for (StockOutcomeBillItem item :stockOutcomeBill.getItems()){
            //为明细设置订单id
            item.setBillId(stockOutcomeBill.getId());
            BigDecimal amount = item.getNumber().multiply(item.getSalePrice()).setScale(2, RoundingMode.HALF_UP);
            item.setAmount(amount);
            //更新明细
            stockOutcomeBillitenMapper.insert(item);
            //计算明细金额小鸡,设置到明细对象中
            totalAmount = totalAmount.add(amount);
            totalNumber = totalNumber.add(item.getNumber());
        }
        //为订单对象设置总金额与数量
        stockOutcomeBill.setTotalAmount(totalAmount);
        stockOutcomeBill.setTotalNumber(totalNumber);
        //保存订单
        stockOutcomeBillMapper.updateByPrimaryKey(stockOutcomeBill);
        //为明细对象设置订单id,保存明细

    }



    public StockOutcomeBill getById(Long id) {
        return stockOutcomeBillMapper.selectByPrimaryKey(id);
    }

    public PageResult query(StockOutcomeBillQueryObject qo) {
        Integer totalCount = stockOutcomeBillMapper.queryForCount(qo);
        if (totalCount == 0) {
            return new PageResult(qo.getPageSize());
        }
        List<StockOutcomeBill> data = stockOutcomeBillMapper.queryForList(qo);
        return new PageResult(totalCount,data,qo.getCurrentPage(),qo.getPageSize());
    }

    public void audit(Long id) {
        System.out.println(id);
        StockOutcomeBill stockOutcomeBill = stockOutcomeBillMapper.selectByPrimaryKey(id);
        System.out.println(stockOutcomeBill);
        if (stockOutcomeBill.getStatus()==stockOutcomeBill.STATUS_NORMAL){
           /* Employee currentUser = UserContext.getCurrentUser();*/
            Employee employee = new Employee();
            employee.setId(1L);
            stockOutcomeBill.setAuditor(employee);
            stockOutcomeBill.setAuditTime(new Date());
            stockOutcomeBill.setStatus(stockOutcomeBill.STATUS_AUDITED);
            stockOutcomeBillMapper.updatebyauditId(stockOutcomeBill);
            //无该货品
           /* for (StockOutcomeBillItem item : stockOutcomeBill.getItems()) {
                ProductStock ps = productStockMapper.selectByproductIdAndDepotId(item.getProduct().getId(), stockOutcomeBill.getDepot().getId());
                if (ps==null){
                    throw  new LoginException("在"+stockOutcomeBill.getDepot().getName()+"仓库中没有"+item.getProduct().getName()+"货品");
                }
                //有库存单是出货量大于她的库存量
                if(ps.getStoreNumber().compareTo(item.getNumber())<0){
                    throw  new LoginException("在"+stockOutcomeBill.getDepot().getName()+"货品在中"+item.getProduct().getName()+"仓库中数量不足");

                }
                //库存量大于或等于的情况
                    ps.setStoreNumber(ps.getStoreNumber().subtract(item.getNumber()));
                    ps.setAmount(ps.getPrice().multiply(ps.getStoreNumber()).setScale(2, RoundingMode.HALF_UP));
                    productStockMapper.updateByPrimaryKey(ps);

            }*/

               productStockService.stockOutcome(stockOutcomeBill);


        }
    }
}
